package com.example.zx.theline.jclr.pub;

import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.DatePicker;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.bigkoo.pickerview.TimePickerView;
import com.example.zx.theline.R;
import com.loopj.android.http.RequestParams;
import com.scu.miomin.shswiperefresh.core.SHSwipeRefreshLayout;
import com.scu.miomin.shswiperefresh.view.SHListView;

import org.apache.http.Header;

import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import GsonBean.AddJclrxxListBean;
import Utils.DateUtil;
import app.MyApplication;
import base.BaseActivity;
import network.NetWorkApi;
import network.ObjectHttpResponseHandler;
import Utils.MyToast;
import retrofit2.Call;
import retrofit2.Callback;
import retrofit2.Response;
import views.SearchBarView;

/**
 * Update by Jianqi on 2017\11\15.
 * <p>
 * 选择计划，新增检查单
 */
public class ZsrjhtxjdjcActivity extends BaseActivity implements View.OnClickListener, SHSwipeRefreshLayout.SHSOnRefreshListener {
    private ImageView back,search;
    DatePicker mDatePicker;
    private SHSwipeRefreshLayout mSHSwipeRefreshLayout;
    private SHListView mListView;
    private TextView mFootTextView, quxiao,sgrq;
    //Data
    private int pageNumber = 0; //页号
    private List<AddJclrxxListBean.RowsBean> mSoureList;
    private MyAdapter mAdapter;
    private Date date;
    private EditText edit;

    //选择正式日计划
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_zsrjhtxjdjc);
        InitView();
        InitData();
        InitListen();
    }

    @Override
    protected void InitView() {
        back = (ImageView) findViewById(R.id.back);
        search = (ImageView) findViewById(R.id.search);
        edit = (EditText) findViewById(R.id.edit);
        mDatePicker = (DatePicker) findViewById(R.id.datapicker);
        mListView = (SHListView) findViewById(R.id.listview);
        mSHSwipeRefreshLayout = (SHSwipeRefreshLayout) findViewById(R.id.swipeRefreshLayout);
        //swipeRefreshLayout footview
        View FootView = View.inflate(this, R.layout.refresh_view, null);
        mFootTextView = (TextView) FootView.findViewById(R.id.title);
        mSHSwipeRefreshLayout.setFooterView(FootView);
        quxiao = (TextView) findViewById(R.id.quxiao);
        sgrq = (TextView) findViewById(R.id.sgrq);
    }

    @Override
    protected void InitData() {
        mSoureList = new ArrayList<>();
        mAdapter = new MyAdapter();
        mListView.setAdapter(mAdapter);
        date=new Date(System.currentTimeMillis());
        //初始化搜索日期
        upDatedDate();
        requestData(0, 10, "refresh");
    }

    @Override
    protected void InitListen() {
        mSHSwipeRefreshLayout.setOnRefreshListener(this);
        back.setOnClickListener(this);
        quxiao.setOnClickListener(this);
        sgrq.setOnClickListener(this);
        search.setOnClickListener(this);
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.back: //顶部返回键
                finish();
                break;
            case R.id.search: //搜索
                requestData(0,10,"refresh");
                break;
            case R.id.quxiao: //取消按钮
                finish();
                break;
            case R.id.sgrq: //日期选择
                TimePickerView.getInstance(this, new TimePickerView.OnTimeSelectListener() {
                    @Override
                    public void onTimeSelect(Date date, View v) {
                        ZsrjhtxjdjcActivity.this.date=date;
                        upDatedDate();
                        requestData(1,10,"refresh");
                    }
                }).show();
                break;
        }
    }
    public void upDatedDate(){
        sgrq.setText(DateUtil.getYearAndMonthAndDay(date));
    }
    @Override
    public void onRefresh() { //初始化数据
        requestData(0, 10, "refresh");
    }

    @Override
    public void onLoading() { //加载数据
        requestData(pageNumber, 10, "loadmore");
    }

    @Override
    public void onRefreshPulStateChange(float percent, int state) {
        switch (state) {
            case SHSwipeRefreshLayout.NOT_OVER_TRIGGER_POINT:
                mSHSwipeRefreshLayout.setRefreshViewText("下拉刷新");
                break;
            case SHSwipeRefreshLayout.OVER_TRIGGER_POINT:
                mSHSwipeRefreshLayout.setRefreshViewText("松开刷新");
                break;
            case SHSwipeRefreshLayout.START:
                mSHSwipeRefreshLayout.setRefreshViewText("正在刷新");
                break;
        }
    }

    @Override
    public void onLoadmorePullStateChange(float percent, int state) {
        switch (state) {
            case SHSwipeRefreshLayout.NOT_OVER_TRIGGER_POINT:
                mFootTextView.setText("上拉加载");
                break;
            case SHSwipeRefreshLayout.OVER_TRIGGER_POINT:
                mFootTextView.setText("松开加载");
                break;
            case SHSwipeRefreshLayout.START:
                mFootTextView.setText("正在加载...");
                break;
        }
    }

    //网络请求列表数据
    private void requestData(int pageNum, int pageSize, final String flag) {
        this.pageNumber = pageNum;
        pageNumber++;
        Map<String, String> map = new HashMap<>();
        map.put("pageNumber", String.valueOf(pageNumber));
        map.put("pageSize", String.valueOf(pageSize));
        map.put("loginDwxxId", MyApplication.getmLoginUser().getUserUnitId());
        map = setMap(map);
        MyApplication.ShowWaitDialog(this);
        NetWorkApi.getZsrjhtxjdjcListDate(map, new Callback<AddJclrxxListBean>() {
                    @Override
                    public void onResponse(Call<AddJclrxxListBean> call, Response<AddJclrxxListBean> response) {
                        try {
                            AddJclrxxListBean mBean = response.body();
                            if (mBean.getMsg().equals("success")) {
                                if (mBean.getRows().size() > 0) {
                                    if (flag.equals("refresh")) { //判断是否是刷新，如果是刷新则将之前的List替换
                                        mSoureList=mBean.getRows();
                                    }
                                    mAdapter.notifyDataSetChanged(); //刷新数据列表
                                } else {
                                    if (flag.equals("loadmore")) {
                                        MyToast.showToast("暂无更多计划数据");
                                    } else {
                                        mSoureList.clear();
                                        mAdapter.notifyDataSetChanged();
                                        MyToast.showToast("当前没有可以新增的计划");
                                    }
                                }
                            } else {
                                MyToast.showNullObjectError(ZsrjhtxjdjcActivity.this);
                            }
                        } catch (Exception e) {
                            MyToast.showFail();
                        }
                        MyApplication.HideWaitDialog();
                        switch (flag) {
                            case "refresh": //刷新
                                mSHSwipeRefreshLayout.finishRefresh();
                                break;
                            case "loadmore": //加载
                                mSHSwipeRefreshLayout.finishLoadmore();
                                break;
                        }

                    }

                    @Override
                    public void onFailure(Call<AddJclrxxListBean> call, Throwable t) {
                        MyToast.showFail();
                    }
                }
        );
    }

    //适配器
    private class MyAdapter extends BaseAdapter {

        @Override
        public int getCount() {
            return mSoureList.size();
        }

        @Override
        public Object getItem(int position) {
            return mSoureList.get(position);
        }

        @Override
        public long getItemId(int position) {
            return position;
        }

        @Override
        public View getView(final int position, View convertView, ViewGroup parent) {
            ViewHolder mVh;
            if (convertView == null) {
                convertView = View.inflate(ZsrjhtxjdjcActivity.this, R.layout.custody_add_adapter, null);
                mVh = new ViewHolder();
                mVh.jhh = (TextView) convertView.findViewById(R.id.jhh);
                mVh.sgrq = (TextView) convertView.findViewById(R.id.sgrq);
                mVh.sgdd = (TextView) convertView.findViewById(R.id.sgdd);
                mVh.sgxm = (TextView) convertView.findViewById(R.id.sgxm);
                mVh.sgnr = (TextView) convertView.findViewById(R.id.sgnr);
                mVh.sgjx = (TextView) convertView.findViewById(R.id.sgjx);
                mVh.blfh = (TextView) convertView.findViewById(R.id.blfh);
                mVh.sgdw = (TextView) convertView.findViewById(R.id.sgdw);
                convertView.setTag(mVh);
            } else {
                mVh = (ViewHolder) convertView.getTag();
            }
            mVh.jhh.setText(mSoureList.get(position).getBh());
            mVh.sgrq.setText(mSoureList.get(position).getSgrq());
            mVh.sgdd.setText(mSoureList.get(position).getSgdd());
            mVh.sgxm.setText(mSoureList.get(position).getXmmc());
            mVh.sgnr.setText(mSoureList.get(position).getNrmc());
            mVh.sgjx.setText(mSoureList.get(position).getJxmc());
            mVh.blfh.setText(mSoureList.get(position).getBlfhmc());
            mVh.sgdw.setText(mSoureList.get(position).getSgdwmc());
            //Item点击跳转
            convertView.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    Intent mIntent = new Intent(ZsrjhtxjdjcActivity.this, JcxxAddActivity.class);
                    Bundle mBundle = new Bundle();
                    mBundle.putString("rjdjhbBh", mSoureList.get(position).getBh());
                    mBundle.putString("jdjhjcbId", null);
                    mIntent.putExtras(mBundle);
                    startActivity(mIntent);
                }
            });
            return convertView;
        }

        class ViewHolder {
            TextView jhh, sgrq, sgdd, sgxm, sgnr, sgjx, blfh, sgdw;
        }
    }

    //设置条件搜索的Post参数
    private Map<String, String> setMap(Map<String, String> map) {
        map.put("searchDate", DateUtil.getYearAndMonthAndDay(date));
        if (!edit.getText().toString().equals("")) {
            map.put("searchBh", edit.getText().toString());
        }
        return map;
    }

}
